package com.ftg.learn.chapter12.springbootdemo.controller;

import com.ftg.learn.chapter12.springbootdemo.entity.PermissionEntity;
import com.ftg.learn.chapter12.springbootdemo.entity.UserEntity;
import com.ftg.learn.chapter12.springbootdemo.service.UserService;
import com.ftg.learn.chapter12.springbootdemo.util.R;
import com.ftg.learn.chapter12.springbootdemo.vo.UserVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 登陆功能
 * @author 布衣
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    UserService userService;

    /**
     * 详细解译一下@RequestParam()传递参数的方式
     * RequestParam springmvc技术的 : 前端传递的参数名称与后端变量映射
     * param  mybatis : 用在mybatis sql参数别名绑定
     * @param username 用户名
     * @param password 密码
     *
     *                 RequestParam 前端 - 以?的方式通过Request(请求) | Response(响应)对象进行传递
     *                 http://localhost:8080/login/login?username=abc&password=123456
     * @return -1 用户名为null -2 密码为空 0 用户不存在 1 登陆成功
     */
    @PostMapping("/login")
    public R<Integer> login(@RequestParam("username") String username,@Param("password") String password){
        return  userService.login(username,password);
    }

    @PostMapping("/permission")
    public R<PermissionEntity> getPermission(@RequestParam("id") Integer id){
        return R.success(null);
    }

    @PostMapping("/getUserInfo")
    public R<UserVo> getUserInfoById( @RequestParam(value = "id",required = true) Integer id){
        return userService.getUserInfoById(id);
    }

    @PostMapping("/modify")
    public R<Boolean> modify(@RequestParam("id") Integer id,@RequestParam("oldpasswrod")String oldpasswrod,
                                @RequestParam("newpasswrod")String newpasswrod){
        return userService.modifyPassword(id,oldpasswrod,newpasswrod);
    }

}
